$(document).ready(function() {
	hotReportAjax();
	$("#generateReprot").click(createReportAjax);
	$("#watch").click(doWatchAjax);
});
var doWatchAjax = function() {
	$.ajax({
		type : 'get',
		url : '../HotSentimentServlet?method=doWatch',
		data : {},
		success : function(data) {

		},
		error : function() {

		}
	});
}
var hotReportAjax = function() {
	$.ajax({
		type : 'GET',
		// url地址 应该是servelet地址
		url : '../HotSentimentServlet?method=getHotspotReports',
		data : {
		// 假设表名为dailypublish
		// "tablename": "dailypublish",
		// "user":"admin",
		// "pagenum":1
		},
		success : function(data) {
			var hotSentiment = JSON.parse(data).reports;
			console.log(hotSentiment);
			if (hotSentiment == null || hotSentiment == "") {
				var infomation = $("<h4>" + "请先生成今日报告！" + "</h4>");
				$("#dailypublishTable").append(infomation);
			} else {
				// var usersData = JSON.parse(data).users;
				$.each(hotSentiment, function(indexs, values) {
					// console.log(this.username);
					var tr = "<tr><td>" + this.title + "</td><td>";
					if (this.keywords != null) {
						for (var i = 0; i < 3; i++) {
							tr += this.keywords[i] + "\\";
						}
					}
					var id = this._id.$oid;
					tr += "</td><td>" + this.date + "</td>"
							+ "<td class=\"center\">"
							+ "<button class=\"btn btn-success\" id=\"" + id
							+ "_checkout\">" + "查看编辑" + "</button>";

					tr += "<button class=\"btn btn-danger\"id=\"" + id
							+ "_selectbtn\">未监测</button>"
					tr += "</td></tr>";
					// 遍历添加行
					$("#hotpublishTable").append(tr);
					// 绑定查看按钮点击事件
					$("#" + id + "_checkout").click(function() {
						console.log(id);
						viewDetail(id);
					})
					// 绑定选择按钮点击事件
					$("#" + id + "_selectbtn").click(function() {
						console.log(id);
						statusAjax(id);
					});

				});
			}
			// pageMaker(recordsTotal);

		},
		error : function() {
			console.log("error");
		}
	});
}
var createReportAjax = function() {
	$.ajax({
		type : "get",
		url : '../HotSentimentServlet?method=createHotspotReport',
		data : {
		// 假设表名为dailypublish
		// "tablename": "dailypublish",
		// "user":"admin",
		// "pagenum":1
		},
		success : function(data) {
			hotReportAjax();
		},
		error : function() {
			console.log("error");
		}
	});
}
function selectStatusCheck(id, status) {
	if (status == 1) {
		console.log("execute here");
		return "<button class=\"btn btn-primary\"id=\"" + id
				+ "_selectbtn\">已监测</button><br>";
	} else if (status == 2) {
		return "<button class=\"btn btn-danger\"id=\"" + id
				+ "_selectbtn\">未监测</button>";
	}
}

function statusAjax(id) {
	$.ajax({
		type : "get",
		url : '../HotSentimentServlet?method=changeReportStatus',
		data : {
			"id" : id,
		},
		success : function(data) {
			console.log(data);

			if (data == 1) {
				$("#" + id + "_selectbtn").removeClass("btn-primary").addClass(
						"btn-danger").html("未监测");
			} else if (data == 2) {
				$("#" + id + "_selectbtn").removeClass("btn-danger").addClass(
						"btn-primary").html("已监测");
			}
		},
		error : function() {
			console.log("ajax transport error");
		}
	});
}

function viewDetail(id) {
	var url = "../WebManagement/";
	location.href = url + "?id=" + id;
}